const app = getApp()


Page({
  data:{
  },
  onLoad:function(params){
    var me = this;
    var redirectUrl = params.redirectUrl;
    redirectUrl = redirectUrl.replace(/#/g, "?");
    redirectUrl = redirectUrl.replace(/@/g, "=");
    me.redirectUrl = redirectUrl;
  },

  doLogin:function(e){
    var me = this;
    var formObject = e.detail.value;
    var username = formObject.username;
    var password = formObject.password;
    if (username.length == 0 || password.length == 0){
      wx.showToast({
        title: '用户名或密码为空 ~',
        icon: "none",
        duration: 3000
      })
    }else {
      var serverUrl = app.serverUrl;
      wx.showLoading({
        title: '请稍等',
      })
      wx.request({
        url: serverUrl + "/login",
        method: "POST",
        data:{
          username:username,
          password:password
        },
        header: {
          "content-type": "application/json"
        },
        success(res){
          console.log(res)
          wx.hideLoading()
          if (res.data.status == 200){
            wx.showToast({
              title: '登录成功',
              icon: "success",
              duration: 2000
            }) 
            // app.userInfo = res.data.data;
            //TODO:修改原有的全局对象为本地缓存
            app.setGloalUserInfo(res.data.data)
            //页面跳转
            var redirectUrl = me.redirectUrl
            if (redirectUrl != '' || redirectUrl != null || redirectUrl != undefined){
              wx.redirectTo({
                url: redirectUrl,
              })
            }else{
              wx.redirectTo({
                url: '../mine/mine',
              })
            }

          }else if(res.data.status == 502){
            wx.showToast({
              title: res.data.msg,
              icon:"none",
              duration:3000
            })
          }
        }
      })
    }
  },
  goRegistPage:function(){
    wx.navigateTo({
      url: '../userRegist/regist',
    })
  }
}) 